package DesignPattern;

import java.util.HashMap;
import java.util.Map;

public class Prototype {
    public static void main(String[] args) {
        ShapeCache.loadcache();

        PrototypeShape shape = ShapeCache.getShape("1");
        System.out.println(shape.getType());

        PrototypeShape shape1 = ShapeCache.getShape("2");
        System.out.println(shape1.getType());
    }
}

abstract class  PrototypeShape implements Cloneable{
    private String id;
    protected String type;

    abstract void draw();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public Object clone(){
        Object clone = null;

        try{
            clone = super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return clone;
    }
}

class circle extends PrototypeShape{

    @Override
    void draw() {
        System.out.println("draw circle");
    }

    public circle(){
        type = "circle";
    }
}

class rectangle extends PrototypeShape{

    @Override
    void draw() {
        System.out.println("draw rectangle");
    }

    public rectangle(){
        type = "rectangle";
    }
}

class ShapeCache{
    private static Map<String, PrototypeShape> map= new HashMap<>();
    public static PrototypeShape getShape(String id){
        return (PrototypeShape) map.get(id).clone();
    }

    public static void loadcache(){
        circle circle = new circle();
        circle.setId("1");
        map.put(circle.getId(), circle);

        rectangle rectangle = new rectangle();
        rectangle.setId("2");
        map.put(rectangle.getId(),rectangle);
    }
}
